package com.example.a16478095zxl;


import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.text.SpannableStringBuilder;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //https://blog.csdn.net/w_mayday/article/details/55106240
    Button btnPlay, btnPause, btnNext, btnPrevious;
    TextView tvProgress;
    SeekBar skMusic;
    MediaPlayer mediaPlayer;
    String musicDuration;
    Handler handler = new Handler();
    int currentMusic = 0;
    int musicList[] = {R.raw.a, R.raw.b, R.raw.c, R.raw.d};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        int[] imageid = new int[]{R.mipmap.ss, R.mipmap.c, R.mipmap.lmdx, R.mipmap.qs };

        String[] name=new String[] {"后来-刘若英","再见某人-某人","连名带姓-尤长靖","KNOWNEWSONGRNB-taylor"};
        List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < imageid.length; i++)
        {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", imageid[i]);

            map.put("name", name[i]);
            listitem.add(map);
        }
        SimpleAdapter adapter = new SimpleAdapter(this, listitem, R.layout.item, new String[]{"name", "image"}, new int[]{R.id.name, R.id.image});
        ListView listView = super.findViewById(R.id.listview);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Map<String, Object> map = (Map<String, Object>) adapterView.getItemAtPosition(i);
                Toast.makeText(MainActivity.this, map.get("name").toString(), Toast.LENGTH_LONG).show();
                if(i==0){
                    if(mediaPlayer==null){
                        mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.a);
                        mediaPlayer.start();
                    }else if(mediaPlayer!=null){
                        stopMusic();
                        mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.a);
                        mediaPlayer.start();
                    }

                }
                if(i==1){
                    if(mediaPlayer==null){
                        mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.b);
                        mediaPlayer.start();
                    }else if(mediaPlayer!=null){
                        stopMusic();
                        mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.b);
                        mediaPlayer.start();
                    }

                }
                if(i==2){
                    if(mediaPlayer==null){
                        mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.c);
                        mediaPlayer.start();
                    }else if(mediaPlayer!=null){
                        stopMusic();
                        mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.c);
                        mediaPlayer.start();
                    }

                }
                if(i==3){
                    if(mediaPlayer==null){
                        mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.d);
                        mediaPlayer.start();
                    }else if(mediaPlayer!=null){
                        stopMusic();
                        mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.d);
                        mediaPlayer.start();
                    }

                }

            }
        });

    }

    private void initEvent() {
        btnPlay.setOnClickListener(this);
        btnPause.setOnClickListener(this);
        btnPrevious.setOnClickListener(this);
        btnNext.setOnClickListener(this);
        skMusic.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                handler.removeCallbacks(run);
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mediaPlayer.seekTo(seekBar.getProgress());
                handler.postDelayed(run,100);
            }
        });
    }

    private void initView() {
        btnPlay = findViewById(R.id.btn_play);
        btnPause = findViewById(R.id.btn_pause);
        btnNext = findViewById(R.id.btn_next);
        btnPrevious = findViewById(R.id.btn_previous);
        tvProgress = findViewById(R.id.tv_progress);
        skMusic = findViewById(R.id.sk_music);
    }

    Runnable run = new Runnable() {
        @Override
        public void run() {
            skMusic.setProgress(mediaPlayer.getCurrentPosition());
            tvProgress.setText(milliSecondsToTimer(mediaPlayer.getDuration() - mediaPlayer.getCurrentPosition()));
            handler.postDelayed(run, 100);
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_play:
                if (mediaPlayer == null) {
                    mediaPlayer = MediaPlayer.create(this, R.raw.a);
                    skMusic.setMax(mediaPlayer.getDuration());
                }
                mediaPlayer.start();
                handler.post(run);
                break;
            case R.id.btn_pause:
                if (mediaPlayer != null)
                    mediaPlayer.pause();
                break;
            case R.id.btn_next:
                stopMusic();
                if(currentMusic==3){
                    currentMusic = 0;
                    Log.d("pxxy","currentMusic==3");
                }else {
                    currentMusic++;
                }
                Log.d("pxxy",currentMusic+"");
                startMusic(currentMusic);
                break;
            case R.id.btn_previous:
                stopMusic();
                if(currentMusic==0){
                    currentMusic = 3;
                }else {
                    currentMusic--;
                }
                Log.d("pxxy",currentMusic+"");
                startMusic(currentMusic);
                break;
        }
    }

    public void startMusic(int currentMusic){
        mediaPlayer = MediaPlayer.create(this, musicList[currentMusic]);
        skMusic.setMax(mediaPlayer.getDuration());
        mediaPlayer.start();
        handler.post(run);
    }

    public void stopMusic(){
        if(mediaPlayer!=null){
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }

    public static String milliSecondsToTimer(long milliseconds) {
        String finalTimerString = "";
        String secondsString = "";

        //Convert total duration into time
        int hours = (int) (milliseconds / (1000 * 60 * 60));
        int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
        int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
        // Add hours if there
        if (hours == 0) {
            //finalTimerString = hours + ":";
        }

        // Pre appending 0 to seconds if it is one digit
        if (seconds == 10) {
            secondsString = "0" + seconds;
        } else {
            secondsString = "" + seconds;
        }

        finalTimerString = finalTimerString + minutes + ":" + secondsString;

        // return timer string
        return finalTimerString;
    }
}
